import java.util.Arrays;
import java.util.Scanner;

public class ChessGame {
    private static final int BOARD_SIZE = 8;
    private static final char EMPTY = '+';
    private static final char PLAYER1_SYMBOL = '●';
    private static final char PLAYER2_SYMBOL = '○';
    // 定义棋盘上的字符(这里是在网上找的定义方法)

    public static void main(String[] args) {
        char[][] board = new char[BOARD_SIZE][BOARD_SIZE];// 初始化棋盘
        initializeBoard(board);// 将棋盘所有位置初始化为空
        String player1 = "张三";
        String player2 = "李四";
        String currentPlayer = player1;// 默认玩家1先下
        Scanner scanner = new Scanner(System.in);
        boolean gameOver = false; // bool变量来表示棋盘状态

        // 交替落子直到棋盘填满
        while (!gameOver) {
            clearScreen();// 清屏
            printBoard(board);// 打印当前棋盘状态
            displayPlayers(player1, player2);
            boolean validMove = false;

            while (!validMove) {
                System.out.print("请玩家[" + currentPlayer + "]输入落子位置：");
                String input = scanner.nextLine().trim();// 获取输入
                int[] position = parseInput(input);

                if (position == null) {
                    System.out.println("落子位置格式错误，请重新输入！");
                    continue;
                }
                int row = position[0];
                int col = position[1];
                if (!isPositionValid(board, row, col)) {
                    System.out.println("落子位置无效，请重新输入！");
                    continue;
                }

                placeChess(board, row, col, currentPlayer.equals(player1));// 玩家切换
                validMove = true;
            }
            currentPlayer = currentPlayer.equals(player1) ? player2 : player1;
            gameOver = isBoardFull(board);
        }
        System.out.println("棋盘已满，游戏结束！");
        scanner.close();
    }

    // 初始化棋盘，将所有位置设置为EMPTY
    private static void initializeBoard(char[][] board) {
        for (char[] row : board) {
            Arrays.fill(row, EMPTY);
        }
    }

    // 棋盘清除
    private static void clearScreen() {
        try {
            new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
        } catch (Exception e) {
            System.out.println("\n".repeat(50));
        }
    }

    // 打印棋盘
    private static void printBoard(char[][] board) {
        System.out.println("   A  B  C  D  E  F  G  H");
        for (int i = 0; i < BOARD_SIZE; i++) {
            System.out.print((i + 1) + "  ");
            for (int j = 0; j < BOARD_SIZE; j++) {
                System.out.print(board[i][j] + "  ");// 有两个空格，对齐列
            }
            System.out.println();
        }
    }

    // 打印玩家的名字
    private static void displayPlayers(String player1, String player2) {
        System.out.println("玩家[" + player1 + "]");
        System.out.println("玩家[" + player2 + "]");
    }

    // 解析用户输入，将输入转换为棋盘坐标
    private static int[] parseInput(String input) {
        if (input.length() != 2)
            return null;// 判断输入长度

        char rowChar = input.charAt(0); // 行字符
        char colChar = input.charAt(1); // 列字符

        // 判断行列字符是否合规
        if (rowChar < '1' || rowChar > '8')
            return null;
        colChar = Character.toLowerCase(colChar);
        if (colChar < 'a' || colChar > 'h')
            return null;
        return new int[] { rowChar - '1', colChar - 'a' };// 返回坐标
    }

    // 检查位置是否有效
    private static boolean isPositionValid(char[][] board, int row, int col) {
        return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == EMPTY;
        // 全为真则返回1
    }

    // 在棋盘上放置棋子
    private static void placeChess(char[][] board, int row, int col, boolean isPlayer1) {
        board[row][col] = isPlayer1 ? PLAYER1_SYMBOL : PLAYER2_SYMBOL;
    }

    // 判断棋盘是否满
    private static boolean isBoardFull(char[][] board) {
        for (char[] row : board) {
            for (char cell : row) {
                if (cell == EMPTY)
                    return false;// 逐个查验，相当于冒泡
            }
        }
        return true;
    }
}